<?php

class Ajax extends APPS_Controller {
	private $tpl;
	
  	function __construct() {
		parent::__construct();
		$this->load->model('siteModel');
		$this->load->model('adminModel');
	}
	
	function index() {
		//$this->login();
	}
	
	function login(){
		$response 	= "0";
		$post		= $this->_post();
		$row 		= $this->siteModel->getLogin($post);
		
		if($row) {
			if ($row['state']== "1"){
				$ip		= $_SERVER['REMOTE_ADDR'];
				
				$this->data['user_name']	= $row['user_name'];
				$this->data['user_id'] 		= $row['user_id'];
				$this->data['acl_id'] 		= $row['acl_id'];
				$this->data['is_auth'] 		= TRUE;
				
				$this->session->set_userdata('is_auth', TRUE);
				$this->session->set_userdata('data', $this->data);

				$sql = "UPDATE app_user set user_last_login = now(), user_last_ip = '$ip'
						WHERE user_id = $row[user_id]";
				
				$this->db->query($sql);

				$response = base_url()."site/akademik/materi_kuliah" ;
				
			} else {
				$response = "1";
			}
		}
		
		echo $response;
	}
	
	function contact(){
		$response 	= true;
		$post 		= $this->_post();
		$row 		= $this->siteModel->contact($post);

		$data['to']		= "$post[email],zola.octanoviar@gmail.com,black_shadow2428@yahoo.com,iman.unforgetable@gmail.com,yuliadi.rumanto@gmail.com";
		//$data['from']	= "'noreply@thera.co.id', 'AMG Info'";
		$data['subject']= "Terima Kasih telah menghubungi kami";
		$data['message']=  "Hi $post[name]  ($post[email]), 
							<br /> Pesan anda : <br />
							$post[message]
							<br />
							<br />

							Terima kasih telah menghubungi kami, pesan anda telah kami terima, <br />
							kami akan menindak lanjuti lebih lanjut.
							<br />
							<br />
							Terima Kasih
							<br />
							Webmaster ( Pengelola Web Site ) AMG";
		
		$response = $this->sentEmail($data);
		echo $response;
	}
	
	function alumni($page=1, $limit=25){
		$post 		= $this->_post();
		$offset   	= $this->setPaging($page, $limit, $this->siteModel->alumni());
		$filter   	= ($post['filter']=='1') ? $post : array();
		$data 		= $this->siteModel->alumni($limit, $offset, $filter);
		
    	$this->smarty->assign('data', $data);
		$this->smarty->display("home/alumni_table.tpl");
	}
	
	function pustaka($page=1, $limit=25){
		$post 		= $this->_post();
		$offset   = $this->setPaging($page, $limit, $this->siteModel->pustaka());
		$filter   = ($post['filter']=='1') ? $post : array();
		$data 		= $this->siteModel->pustaka($limit, $offset, $filter);

    $this->smarty->assign('data', $data);
		$this->smarty->display("home/perpustakaan_table.tpl");
	}
	
    function _post() {
		$post = array();
		foreach($_POST as $k=>$v) {
			if(is_array($_POST[$k])) {
				$index = $this->input->post($k);
				foreach($index as $x=>$y) {
					$value 		  = $y ? $y : null;
					$post[$k][$x] = addslashes($value);
				}
			}
			else {
				$value 	  = $this->input->post($k) ? $this->input->post($k) : null;
				$post[$k] = addslashes($value);
			}
		}
		return $post;
	}
	
	function pinValidation(){
		//$response 	= "0";
		$pin		= $this->input->post('pin');
		
		$this->load->helper(array('form', 'url'));
		$this->load->library('Form_validation');
		
		$this->form_validation->set_rules('pin', 'pin', 'trim|required');
		$row = $this->siteModel->pinValidation($pin);

		if ($pin == ""){
			redirect("site/pin_failed/empty");
			
		/* jika pin ada dalam database */
		} else if($row) {	
			if ($row['state']== "0"){
				$this->data['pin'] = $row['pin'];
				$this->data['state'] = 0;
				
				/*
				$sql = "UPDATE pin set state = 1
						WHERE pinID = $row[pinID]";
				
				// Update PIN
				$this->db->query($sql);
				*/
				
				//----
				//$response = base_url()."site/informasi/form_pendaftaran" ;
				//redirect("site/informasi/form_pendaftaran");
				redirect("site/pin_success/". $pin);
				$response = "Pin Sukses";
				
			} else {
				redirect("site/pin_failed/used");
			}
			
		} else {
			redirect("site/pin_failed/wrong");
		}
		
	}
	
	
	function pendaftaranUpdate(){
		
		
		
		$pin              	= $this->input->post('pin');
		$confirm           	= $this->input->post('confirm');
		$no_pendaftaran		= $this->input->post('no_pendaftaran');
		$nama				= $this->input->post('nama');
		$tempat_lahir		= $this->input->post('tempat_lahir');
		$tgl_lahir_dd		= $this->input->post('tgl_lahir_dd');
		$tgl_lahir_mm		= $this->input->post('tgl_lahir_mm');
		$tgl_lahir_yy		= $this->input->post('tgl_lahir_yy');		
		$jenis_kelamin 		= (strlen($this->input->post('jenis_kelamin')) > 0) ? $this->input->post('jenis_kelamin') : "NULL";
		$tinggi_badan		= $this->input->post('tinggi_badan');
		$agama				= $this->input->post('agama');
		$propinsiID			= $this->input->post('propinsiID');
		$tahun_lulus		= $this->input->post('tahun_lulus');
		$jenis_smtaID		= $this->input->post('jenis_smtaID');
		$jurusan_smtaID		= $this->input->post('jurusan_smtaID');
		$jurusanID			= $this->input->post('jurusanID');
		$lokasiID			= $this->input->post('lokasiID');
		$mat_4				= $this->input->post('mat_4');
		$fisika_4			= $this->input->post('fisika_4');
		$inggris_4			= $this->input->post('inggris_4');
		$mat_5				= $this->input->post('mat_5');
		$fisika_5			= $this->input->post('fisika_5');
		$inggris_5			= $this->input->post('inggris_5');
		$mat_6				= $this->input->post('mat_6');
		$fisika_6			= $this->input->post('fisika_6');
		$inggris_6			= $this->input->post('inggris_6');
		$register_date		= $this->input->post('register_date');
		$email				= $this->input->post('email');
		$ayah					= $this->input->post('ayah');
		$ibu					= $this->input->post('ibu');
		$alamat				= $this->input->post('alamat');
		$kodepos			= $this->input->post('kodepos');
		$telepon			= $this->input->post('telepon');
		$image_1 			= $_FILES['foto'];
		$image_path 		= "./assets/images/foto_calon_taruna/";
		$image_Fotopath = "./assets/images/foto_calon_taruna/";
		$insert_image_1 	= "";
		$emailNardi    		= "nardiamg@gmail.com";
		$agree				= (strlen($this->input->post('agree')) > 0) ? $this->input->post('agree') : "NULL";
		$date				= $tgl_lahir_dd."-".$tgl_lahir_mm."-".$tgl_lahir_yy;
		
		$lastID				= $this->siteModel->getLastID();
		
		$no_pendaftaran		= $lokasiID. ($lastID['id'] + 1);
		
		$update 			= "";
		$sql 				= "";
		$img_1				= "";
		
		
		$this->load->helper(array('form', 'url'));
		$this->load->library('Form_validation');

		//$this->form_validation->set_rules('pin', 'pin', 'trim|required');
	    $this->form_validation->set_rules('nama', 'nama', 'trim|required');
	 	$this->form_validation->set_rules('tempat_lahir', 'tempat_lahir', 'trim|required');
	 	$this->form_validation->set_rules('tgl_lahir_dd', 'tgl_lahir_dd', 'trim|required');
		$this->form_validation->set_rules('tgl_lahir_mm', 'tgl_lahir_mm', 'trim|required');
		$this->form_validation->set_rules('tgl_lahir_yy', 'tgl_lahir_yy', 'trim|required');
		$this->form_validation->set_rules('jenis_kelamin', 'jenis_kelamin', 'trim|required');		
		$this->form_validation->set_rules('tinggi_badan', 'tinggi_badan', 'trim|required');				
		$this->form_validation->set_rules('agama', 'agama', 'trim|required');		
		$this->form_validation->set_rules('propinsiID', 'propinsiID', 'trim|required');		
		$this->form_validation->set_rules('tahun_lulus', 'tahun_lulus', 'trim|required');		
		$this->form_validation->set_rules('jenis_smtaID', 'jenis_smtaID', 'trim|required');		
		$this->form_validation->set_rules('jurusan_smtaID', 'jurusan_smtaID', 'trim|required');		
		$this->form_validation->set_rules('jurusanID', 'jurusanID', 'trim|required');		
		$this->form_validation->set_rules('lokasiID', 'lokasiID', 'trim|required');		
		$this->form_validation->set_rules('mat_4', 'mat_4', 'trim|required');				
		$this->form_validation->set_rules('fisika_4', 'fisika_4', 'trim|required');				
		$this->form_validation->set_rules('inggris_4', 'inggris_4', 'trim|required');				
		$this->form_validation->set_rules('mat_5', 'mat_5', 'trim|required');				
		$this->form_validation->set_rules('fisika_5', 'fisika_5', 'trim|required');				
		$this->form_validation->set_rules('inggris_5', 'inggris_5', 'trim|required');				
		$this->form_validation->set_rules('mat_6', 'mat_6', 'trim|required');				
		$this->form_validation->set_rules('fisika_6', 'fisika_6', 'trim|required');				
		$this->form_validation->set_rules('inggris_6', 'inggris_6', 'trim|required');				
		$this->form_validation->set_rules('email', 'email', 'trim|required');
		$this->form_validation->set_rules('ayah', 'ayah', 'trim|required');
		$this->form_validation->set_rules('ibu', 'ibu', 'trim|required');
		$this->form_validation->set_rules('alamat', 'alamat', 'trim|required');
		$this->form_validation->set_rules('kodepos', 'kodepos', 'trim|required');
		$this->form_validation->set_rules('telepon', 'telepon', 'trim|required');
		$this->form_validation->set_rules('agree', 'agree', 'trim|required');
				
		
		if($image_1!=null){
			move_uploaded_file($image_1["tmp_name"], $image_path.$image_1["name"]);
			
			if ( $image_1["name"] != null )
				$this->createFotoCalonTaruna( $image_1["name"] );
				
			if (strlen( $image_1["name"]) > 0 ) {
				$img_1 	= "assets/images/foto_calon_taruna/". $image_1["name"];
			}
		}
						
		if ($this->form_validation->run() == FALSE){
			redirect("site/form_required/field_required/".$pin);
			//$response="isi smua field bertanda *";
/*
		} else if ($confirm == 0) {
			redirect("site/form_required/confirm/".$_POST);
*/			
		} else {
			// cek email
			$url_detail = base_url()."site/pendaftaranDetail/".$no_pendaftaran;
			
			if($email != null){
				$row = $this->siteModel->getSameEmail($email);
				
				if($row == NULL ) {
					
					$response = true;
					$data['from']		=	'registrasi@amg.ac.id - Panitia Seleksi Penerimaan Calon Taruna Baru';
					$data['to']			=	$email.', '. $emailNardi;
					$data['subject']	=	'Akademi Meteorologi dan Geofisika - Konfirmasi Pendaftaran Calon Taruna Baru';
					$data['message']	=	'Kepada, '.$nama.
											'<br /><br />
											Anda telah berhasil terdaftar sebagai Calon Taruna Baru - 
											Akademi Meteorologi dan Geofisika Tahun 2010/2011<br /> 
											No. Pendaftaran Anda : '.$no_pendaftaran.
											'<br />Klik Link di bawah ini untuk melihat detail pendaftaran :<br /><br />'.
											$url_detail.'<br />
											<br />Terima Kasih<br />
											<br />Panitia SIPENCATAR 2010/2011<br />';
					
					$response = $this->sentEmail($data);
					//--
						
					// disable PPIN
					$sql = "UPDATE pin set state = 1
							WHERE pin = $pin";

					$this->db->query($sql);
					//----
				
					// insert record			
					$sql = "INSERT INTO student_register(
								no_pendaftaran, nama, tempat_lahir, tgl_lahir, jenis_kelamin, tinggi_badan, agama,
								propinsiID, tahun_lulus, jenis_smtaID, jurusan_smtaID, jurusanID, lokasiID, 
								mat_4, fisika_4, inggris_4, mat_5, fisika_5, inggris_5, mat_6, fisika_6, inggris_6, register_date, 
								email, ayah, ibu, alamat, kodepos, telepon, foto, pin)
							
							VALUES (
								'$no_pendaftaran', '$nama', '$tempat_lahir', '$date', '$jenis_kelamin', '$tinggi_badan', '$agama',
								'$propinsiID', '$tahun_lulus', '$jenis_smtaID', '$jurusan_smtaID', '$jurusanID', '$lokasiID', 
								'$mat_4', '$fisika_4', '$inggris_4', '$mat_5', '$fisika_5', '$inggris_5', '$mat_6', '$fisika_6', '$inggris_6', now(), 
								'$email', '$ayah', '$ibu', '$alamat', '$kodepos', '$telepon', '$img_1', '$pin')";
					//-----
						
					if (strlen($sql)> 0 ) {
						$this->db->query($sql);
						redirect("site/form_success/".$no_pendaftaran);
						//$response="success";
					}
				
				} else {
						//$response = "Email yang anda masukkan telah terdaftar";
						redirect("site/form_required/invalid_email/".$pin);
			}
		}

		echo $response; 
}
						
	}
		
		function createFotoCalonTaruna( $image ) {
		$width = 113;
		$height= 137;
	
		$source = ImageCreateFromJPEG( base_url()."assets/images/foto_calon_taruna/".$image );
		$imgFoto = imagecreatetruecolor($width, $height);
		$image_Fotopath = "./assets/images/foto_calon_taruna/";

		
		// resize image
		imagecopyresampled($imgFoto, $source, 0, 0, 0, 0, $width, $height, imagesx($source), imagesy($source));
		
		// save file to directory foto_calon_taruna img
		imageJPEG( $imgFoto, $image_Fotopath.$image, 100 );
		
		// clear up memory mi
		imagedestroy($source);
		imagedestroy($imgFoto);
	}
	
	function printPdf($no_pendaftaran=0, $order='no_pendaftaran', $sort='asc', $page=1, $limit=50, $msg="") {
		
		$this->data['order']	= $order;
		$this->data['sort']		= $sort;
		$this->data['page']		= $page;
		$this->data['limit']	= $limit;
		$this->data['msg']		= $msg;

		//$this->data['back']	= $this->site['site']."admin/pendaftaran/$order/$sort/$page/$limit" ;
		$this->data['row'] 		= $this->siteModel->getPendaftaranDetail( $no_pendaftaran );
		
		// echo count($this->data['row']);))
		if (count($this->data['row'])==0) {
			$this->data['row']['no_pendaftaran']		= "";
			$this->data['row']['nama'] 					= "";
			$this->data['row']['tempat_lahir']			= "";
			$this->data['row']['tgl_lahir']				= "";
			$this->data['row']['jenis_kelamin']			= "";
			$this->data['row']['tinggi_badan'] 			= "";
			$this->data['row']['agama']					= "";
			$this->data['row']['propinsi_smtaID'] 		= "";
			$this->data['row']['tahun_lulus']			= "";
			$this->data['row']['jenis_smtaID']			= "";
			$this->data['row']['jurusan_smtaID']		= "";
			$this->data['row']['jurusanID']				= "";
			$this->data['row']['lokasiID']				= "";
			$this->data['row']['mat_4']					= "";
			$this->data['row']['fisika_4']				= "";
			$this->data['row']['inggris_4']				= "";
			$this->data['row']['mat_5']					= "";
			$this->data['row']['fisika_5']				= "";
			$this->data['row']['inggris_5']				= "";
			$this->data['row']['mat_6']				= "";
			$this->data['row']['fisika_6']			= "";
			$this->data['row']['inggris_6']			= "";
			$this->data['row']['register_date']		= "";
			$this->data['row']['email']				= "";
			$this->data['row']['alamat']			= "";
			$this->data['row']['kodepos']			= "";
			$this->data['row']['telepon']			= "";
		} else {
			//
		}
		
		//$data = $this->data;
		$this->smarty->assign('data', $this->data);
		$this->load->helper('to_pdf');
			     
		$data = $this->data;     
		$html = $this->load->view('pendaftaran_detail_2',$data, true);
		     
		pdf_create($html, 'pendaftaran_'.$no_pendaftaran);
	}  
	
	function printPdfTagihan($no_pendaftaran=0, $order='no_pendaftaran', $sort='asc', $page=1, $limit=50, $msg="") {
		
		$this->data['order']	= $order;
		$this->data['sort']		= $sort;
		$this->data['page']		= $page;
		$this->data['limit']	= $limit;
		$this->data['msg']		= $msg;

		//$this->data['back']	= $this->site['site']."admin/pendaftaran/$order/$sort/$page/$limit" ;
		$this->data['row'] 		= $this->siteModel->getPendaftaranDetail( $no_pendaftaran );
		
		// echo count($this->data['row']);))
		if (count($this->data['row'])==0) {
			$this->data['row']['no_pendaftaran']	= "";
			$this->data['row']['nama'] 				= "";
			$this->data['row']['tempat_lahir']		= "";
			$this->data['row']['tgl_lahir']			= "";
			$this->data['row']['jenis_kelamin']		= "";
			$this->data['row']['tinggi_badan'] 		= "";
			$this->data['row']['agama']				= "";
			$this->data['row']['propinsi_smtaID']	= "";
			$this->data['row']['tahun_lulus']		= "";
			$this->data['row']['jenis_smtaID']		= "";
			$this->data['row']['jurusan_smtaID']	= "";
			$this->data['row']['jurusanID']			= "";
			$this->data['row']['lokasiID']			= "";
			$this->data['row']['mat_4']				= "";
			$this->data['row']['fisika_4']			= "";
			$this->data['row']['inggris_4']			= "";
			$this->data['row']['mat_5']				= "";
			$this->data['row']['fisika_5']			= "";
			$this->data['row']['inggris_5']			= "";
			$this->data['row']['mat_6']				= "";
			$this->data['row']['fisika_6']			= "";
			$this->data['row']['inggris_6']			= "";
			$this->data['row']['register_date']		= "";
			$this->data['row']['email']				= "";
			$this->data['row']['alamat']			= "";
			$this->data['row']['kodepos']			= "";
			$this->data['row']['telepon']			= "";
			$this->data['row']['BILLING_NO']		= "";
		} else {
			//
		}
		
		//$data = $this->data;
		$this->smarty->assign('data', $this->data);
		$this->load->helper('to_pdf');
			     
		$data = $this->data;     
		$html = $this->load->view('pendaftaran_detail_tagihan',$data, true);
		     
		pdf_create($html, 'pendaftaran_'.$no_pendaftaran);
	}  
	
	function pendaftaranNext($post) {
		$_POST = $post;
	}
			
}